import dayjs from 'dayjs'
/**
 * @description 处理首字母大写
 */
export const upperStr = (str: string) => {
  return str.charAt(0).toUpperCase() + str.slice(1)
}
/**
 * @description 复制文本
 */
export const copyText = (text: string) => {
  return new Promise((resolve, reject) => {
    const copyInput = document.createElement('input')
    copyInput.value = text
    document.body.appendChild(copyInput)
    copyInput.select()
    document.execCommand('copy')
    copyInput.remove()
    resolve(true)
  })
}
/**
 * @description 判断是否是base64格式
 */
export const isBase64 = (str: string): boolean => {
  if (str === '' || str.trim() === '') {
    return false
  }
  try {
    return btoa(atob(str)) == str
  } catch (err) {
    return false
  }
}
/**
 * 对象转json
 */
export const toJSON = (obj) => {
  return JSON.stringify(obj, (key, value) => {
    switch (true) {
      case typeof value === 'undefined':
        return 'undefined'
        break
      case typeof value === 'symbol':
        return value.toString()
      case typeof value === 'function':
        return value.toString()
      default:
        break
    }
    return value
  })
}
/**
 * @description 是否是生产环境
 */
export const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV!)
export const IS_DEV = ['development'].includes(process.env.NODE_ENV!)
/**
 * @description 日期格式化
 */
export const formatDate = (time) => dayjs(time).format('YYYY-MM-DD HH:mm:ss')
/**
 * @description 将一维数组转成树形结构
 */
export const generateTree = (items, id = 0, link = 'parent') => {
  return items
    .filter((item) => item[link] == id)
    .map((item) => ({
      ...item,
      slots: { title: 'name' },
      children: generateTree(items, item.departmentid)
    }))
}
/**
 * 转换驼峰
 */
export const toHump = (name) => {
  return name
    .replace(/[\-\/\_](\w)/g, (all, letter) => {
      return letter.toUpperCase()
    })
    .replace('views', '')
}
